package cn.JHai.rbac.controller;


import cn.JHai.rbac.RequiredPermission;
import cn.JHai.rbac.domain.Department;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    /**
     * 显示department，运用了分页
     * @param queryObject
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @RequiredPermission({"获取部门","department:list"})
    public String list(QueryObject queryObject , Model model){
        PageResult result = departmentService.query(queryObject);

        model.addAttribute("result",result);
        return "/department/list";
    }

    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @RequiredPermission({"删除部门","department:delete"})
    public String list(  Model model,Long id){
        departmentService.Delete(id);
        return "redirect:/department/list";
    }


    @RequestMapping("input")

    public String input(Model model,Long id){
        if(id!=null){
            model.addAttribute("entity",departmentService.get(id));
        }
        return "department/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission({"更新和添加部门","department:saveOrUpdate"})
    public String saveOrUpdate(Department department){
        departmentService.saveOrUpdata(department);
        return "redirect:/department/list";
    }
}
